import os
import sys

PRE_COMMIT_MSG_HOOKSRC = '''#!/bin/sh
python ./prepare-commit-msg.py $*
'''
# COMMIT_MSG_HOOKSRC = '''#!/bin/sh
# python ./commit-msg.py $*
# '''


def path_compi(tpath):
    return tpath.replace("/", os.sep)


def touch_src(pathproj, hookf, hooksrc):
    tdir = path_compi("%s/.git/hooks" % pathproj)
    if not os.path.isdir(tdir):
        print("Not find .git/hooks dir!")
        sys.exit()
    tf = path_compi("%s/%s" % (tdir, hookf))
    if os.path.isfile(tf):
        os.remove(tf)

    with open(tf, "w") as hksrc:
        hksrc.write(hooksrc)
        hksrc.close()
        os.system("chmod a+x %s" % tf)
        print("Init %s suc!" % hookf)


if __name__ == "__main__":
    touch_src(os.getcwd(), "prepare-commit-msg", PRE_COMMIT_MSG_HOOKSRC)
